package com.vf.editor.mvc.resources.icon;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import com.jfinal.aop.Before;
import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.Icon;
import com.vf.editor.common.model.resources.IconClassify;
import com.vf.editor.common.plugins.shiro.ShiroKit;
import com.vf.editor.common.plugins.shiro.SimpleUser;
import com.vf.editor.common.util.UuidUtil;

@Path(value = "/resources/icon", viewPath = "/views/resources/icon/")
public class IconController extends BaseController {
	
	public void index() {
		this.render("list.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String name = getPara("name");
		String classifyId=getPara("classifyId","1");
		
		String sqlExceptSelect = " from " + Icon.TABLE_NAME + " o  where 1=1 ";
		
		if (!StrKit.isBlank(name)) {
			sqlExceptSelect += " and o.name like '%" + name + "%'  ";
		}
		if (!StrKit.isBlank(classifyId)) {
			sqlExceptSelect += " and o.classifyId = '"+classifyId+"' ";
		}
		
		sqlExceptSelect += "  order by   o.createTime desc ";
		Page<Icon> page = Icon.dao.paginate(pageNumber, pageSize, "select o.* ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

	public void allList() {
		List<Icon> Icons = Icon.dao.findAll();
		if(Icons!=null && Icons.size()>0) {
			renderSuccess("查询成功！", Icons);
		}else {
			renderError("无数据！");
		}
	}
	
	public void save() {
		Icon model = getModel(Icon.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				renderSuccess("更新成功！", model);
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！", model);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() {
		Icon model = Icon.dao.findById(this.getPara("id"));
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！");
				if(!StrKit.isBlank(model.getUrl())) {
					File file = new File(rootPath+model.getUrl());
					if (file.exists()) {
						file.delete();
					}
				}
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
	public void getById() {
		Icon model = Icon.dao.findById(this.getPara("id"));
		if (model != null) {
			renderSuccess("成功！",model);
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}

	private String rootPath = PathKit.getWebRootPath();
	public void upload() {
		UploadFile uploadFile = getFile();
		try {
			String classifyId=getPara("classifyId","1");
			IconClassify modelClassify=IconClassify.dao.findById(classifyId);
			if(modelClassify==null) {
				renderError("无效分组，请检查是否存在!");
				return ;
			}
			SimpleUser simpleUser = ShiroKit.getLoginUser();
			
			String path = "/upload/icon" + "/" + uploadFile.getFileName().replace(" ", "");
			File file = new File(rootPath+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			java.awt.image.BufferedImage image = ImageIO.read(uploadFile.getFile());
			double width = image.getWidth();
			double height = image.getHeight();
			uploadFile.getFile().renameTo(file);
			Icon icon = new Icon();
			icon.setId(UuidUtil.getUUID());
			icon.setName(uploadFile.getFileName());
			icon.setCreateTime(new Date());
			icon.setWidth(width);
			icon.setHeight(height);
			icon.setUrl(path);
			icon.setClassifyId(classifyId);
			icon.setCreateTime(new Date(System.currentTimeMillis()));
			icon.setCreateUserId(simpleUser==null?"":simpleUser.getId());
			icon.setCreateUserName(simpleUser==null?"":simpleUser.getId());
			if(icon.save()) {
				renderSuccess("上传成功！",icon);
			}else {
				renderError("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
	public void saveClassify() {
		IconClassify model = getModel(IconClassify.class);
		if (StrKit.notBlank(model.getId()) ) {
			if (model.update()) {
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！",model);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void getClassifys() {
		List<IconClassify> classifies=IconClassify.dao.find("SELECT * FROM "+IconClassify.TABLE_NAME+" ORDER BY SORT ASC ");
		if (classifies!=null) {
			renderSuccess("成功！",classifies);
		} else {
			renderError("暂无分类！");
		}
	}
	
	public void listDataForClassify() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		String sqlExceptSelect = " FROM " + IconClassify.TABLE_NAME + " o  WHERE 1=1 ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keyword + "%'  ) ";
		}
		sqlExceptSelect += "  ORDER BY   O.SORT ASC ";
		Page<IconClassify> page = IconClassify.dao.paginate(pageNumber, pageSize, "select * ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}
	
	
	
	@Before(Tx.class)
	public void deleteClassify() throws Exception {
		String id = getPara("id");
		IconClassify model = IconClassify.dao.findById(id);
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！",model);
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}

}
